$(function() {
    getUserInfo()
        // 点击退出，然后跳转到登录页面
    $('#btnLogin').click(function() {
        layer.confirm('是否确认退出?', { icon: 3, title: '提示' }, function(index) {
            //do something
            localStorage.removeItem('token')
            location.href = '/login.html'
            layer.close(index);
        });
    })
})


function getUserInfo() {
    $.ajax({
        method: "GET",
        url: "/my/userinfo",
        success: function(res) {
            if (res.status !== 0) {
                return layui.layer.msg('获取信息失败！')
            }
            renderAvatar(res.data)
        },
    })
}

function renderAvatar(data) {
    const name = data.nickname || data.username
    const first = name[0].toUpperCase()
    if (data.user_pic !== null) {
        $('.layui-nav-img').attr('src', data.user_pic).show()
        $('.text-avatar').hide()
    } else {
        $('.text-avatar').html(first).show()
        $('.layui-nav-img').hide()
    }
    $('#welcome').html('欢迎&nbsp;&nbsp;' + name)
}

// function skipArtCate() {
//     // console.log(1);
//     $('#art_cate_list')[0].click()

// }